﻿using Microsoft.Extensions.Options;
using System.Threading.Tasks;
using TimServerSDK.Core;
using TimServerSDK.Abp.Settings;
using Volo.Abp.Settings;

namespace TimServerSDK.Core
{
    public class AbpTimServerOptionsFun : ITimServerOptionsFun
    {
        private readonly TimServerOptions _options;
        private readonly ISettingProvider _settingProvider;
        public AbpTimServerOptionsFun(IOptions<TimServerOptions> options, ISettingProvider settingProvider)
        {
            _options = options.Value;
            _settingProvider = settingProvider;
        }

        public async Task<TimServerOptions> GetOptionsAsync()
        {
            var options = _options;
            if (options == null)
            {
                options = new TimServerOptions();
                var appId  = await _settingProvider.GetOrNullAsync(TimeServerSDKSettings.AppId);
                int.TryParse(appId,out int r);
                options.AppId = r;
                options.AppSecret = await _settingProvider.GetOrNullAsync(TimeServerSDKSettings.AppSecret);
                options.Identifier = await _settingProvider.GetOrNullAsync(TimeServerSDKSettings.Identifier);
            }
            return options;
        }
    }
}
